package com.wise.site.main;

import com.wise.site.R;
import com.wise.site.WiseSiteApplication;
import com.wise.site.adapter.ArrayListAdapter;
import com.wise.site.adapter.BusinessAdapter;
import com.wise.site.adapter.BusinessAdapter.BUSINESS_TYPE;
import com.wise.site.adapter.IndexerBarAdapter;
import com.wise.site.detail.BuyDetailsActivity;
import com.wise.site.detail.SupplyDetailsActivity;
import com.wise.site.protocol.action.BusinessAction;
import com.wise.site.protocol.base.ProtocolManager;
import com.wise.site.protocol.base.SoapAction.ACTION_TYPE;
import com.wise.site.protocol.base.SoapAction.ActionListener;
import com.wise.site.protocol.result.BussinesItemsResult;
import com.wise.site.protocol.result.BussnissItem;
import com.wise.site.protocol.result.IndexerBarEntry;
import com.wise.site.protocol.result.LoginResult;
import com.wise.site.protocol.result.ModelResult;
import com.wise.site.protocol.result.TabItem;
import com.wise.site.utils.Constanct;
import com.wise.site.utils.DataCache;
import com.wise.site.widget.IndexerBarGallery;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

/**
 * 供求页面
 * @author huangyue
 *
 * 2012-9-20
 */
public class BusinessActivity extends Activity implements OnClickListener{
	private IndexerBarAdapter adapter;
	private ListView listView;
	private BusinessAdapter mBuyAdapter;
	private BusinessAdapter mSaleAdapter;
	private IndexerBarGallery indexerBar;
	private TabItem tab ;
	
	private int type;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.main_bussion);
		tab = WiseSiteApplication.getContext().getModelResult().getTabItem(2);
		initGallery();
		listView = (ListView) findViewById(R.id.list);
		mSaleAdapter = new BusinessAdapter(BUSINESS_TYPE.SALE_TYPE, this);
		mBuyAdapter = new BusinessAdapter(BUSINESS_TYPE.BUY_TYPE, this);
		mSaleAdapter.setListView(listView);
		
		if(DataCache.getInstance().getIntByKey(Constanct.INFO_TYPE, -1) == -1){
			DataCache.getInstance().put(Constanct.INFO_TYPE, 0);
		}
		
		findViewById(R.id.sale_btn).setOnClickListener(this);
		findViewById(R.id.buy_btn).setOnClickListener(this);
		findViewById(R.id.search_input).setOnClickListener(this);
		
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View v, int index,
					long id) {
				BussnissItem item = (BussnissItem) v.getTag();
				BusinessAdapter adapter = type == 0 ? mSaleAdapter : mBuyAdapter;
				if(item != null && adapter != null){
					Intent intent = null;
					if(adapter.getType() == BUSINESS_TYPE.SALE_TYPE){
						intent = new Intent(getApplicationContext(), SupplyDetailsActivity.class);
					}else{
						intent = new Intent(getApplicationContext(), BuyDetailsActivity.class);
					}
					intent.putExtra(Constanct.INFO_ENTRY, item);
					
					startActivity(intent);
				} 
			}
		});
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		int tyep = DataCache.getInstance().getIntByKey(Constanct.INFO_TYPE, -1);
		indexerBar.setSelection(Integer.MAX_VALUE / 2 - ((Integer.MAX_VALUE / 2)  % tab.indexBarList.size()));
		if(tyep == 0){
			toSale();
		} else if(tyep == 1){
			toBuy();
		}
		
		DataCache.getInstance().put(Constanct.INFO_TYPE, -1);
	}
	
	private void initGallery() {
		if(tab.indexBarList == null || tab.indexBarList.size() == 0){
			return;
		}
		indexerBar = (IndexerBarGallery) findViewById(R.id.index_gallery);
		indexerBar.setTabId(tab.id);
		adapter = new IndexerBarAdapter(this);
		adapter.setList(tab.indexBarList);
		indexerBar.setAdapter(adapter);
//		indexerBar.setSelection(Integer.MAX_VALUE / 2 - ((Integer.MAX_VALUE / 2)  % tab.indexBarList.size()));
		indexerBar.setOnItemClickListener(new OnItemClickListener(){
			public void onItemClick(AdapterView<?> arg0, View v, int arg2,
					long arg3) {
				indexerBar.setSelector(v);
//				LoginResult login = (LoginResult) DataCache.getInstance().get(Constanct.ISLOGINED_KEY);
				
				BusinessAdapter adapter = type == 0 ? mSaleAdapter : mBuyAdapter;
				if(adapter != null){
//					if(login == null){
						adapter.setMemberID(0);
//					} else {
//						adapter.setMemberID(login.id);
//					}
					IndexerBarEntry entry = (IndexerBarEntry) v.getTag();
					adapter.setStype(entry.id);
					adapter.moveToFristPage();
					adapter.loadData();
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		if(tab.indexBarList == null || tab.indexBarList.size() == 0){
			return;
		}
		
		switch (v.getId()) {
		case R.id.sale_btn:{
//			indexerBar.setFrist(true);
//			findViewById(R.id.search_input_p).setVisibility(View.VISIBLE);
			indexerBar.setSelection(Integer.MAX_VALUE / 2 - ((Integer.MAX_VALUE / 2)  % tab.indexBarList.size()));
			toSale();
			break;
		}
		case R.id.buy_btn:{
//			indexerBar.setFrist(true);
//			findViewById(R.id.search_input_p).setVisibility(View.GONE);
			indexerBar.setSelection(Integer.MAX_VALUE / 2 - ((Integer.MAX_VALUE / 2)  % tab.indexBarList.size()));
			toBuy();
			break;
		}	
		case R.id.search_input:{
			BusinessAdapter adapter = type == 0 ? mSaleAdapter : mBuyAdapter;
			if(adapter != null){
				Intent intent = new Intent(getApplicationContext(), SearchActivity.class);
				intent.putExtra(Constanct.INTENT_SEARCH_TYPE, adapter.getType() == BUSINESS_TYPE.SALE_TYPE ? 0 : 2);
				startActivity(intent);
			}
			break;
		}
		default:
			break;
		}
		
	}

	private void toBuy() {
		mBuyAdapter.setListView(listView);
		type = 1;
		
		IndexerBarEntry entry = tab.indexBarList.get(0);
		int i = 0;
		for(IndexerBarEntry e : tab.indexBarList){
			if(e.type == 2){
				entry = e;
				break;
			}
			i++;
		}
		mBuyAdapter.setMemberID(0);
		mBuyAdapter.setStype(entry.id);
		mBuyAdapter.moveToFristPage();
		mBuyAdapter.loadData();
		indexerBar.toIndex(i);
		findViewById(R.id.buy_btn).setBackgroundResource(R.drawable.abuy_click);
		findViewById(R.id.sale_btn).setBackgroundResource(R.drawable.supply_normal);
	}

	private void toSale() {
		mSaleAdapter.setListView(listView);
		type = 0;
		IndexerBarEntry entry = tab.indexBarList.get(0);
		int i = 0;
		for(IndexerBarEntry e : tab.indexBarList){
			if(e.type == 2){
				entry = e;
				break;
			}
			i++;
		}
		mSaleAdapter.setMemberID(0);
		mSaleAdapter.setStype(entry.id);
		mSaleAdapter.moveToFristPage();
		mSaleAdapter.loadData();
		indexerBar.toIndex(i);
		findViewById(R.id.buy_btn).setBackgroundResource(R.drawable.abuy_normal);
		findViewById(R.id.sale_btn).setBackgroundResource(R.drawable.supply_click);
	}
}
